package com.dynamic;

public class ClimbStairs {
    /**
     * 线性递归
     * 缺点：栈深度 太深
     *
     * @param n
     * @return
     */
    public int climbStairs(int n) {
        if (n < 3) return n;

        return climbStairs(n - 1) + climbStairs(n - 2);
    }

    /**
     * 动态规划
     */
    public int climbStairs2(int n) {
        if (n <= 3) return n;

        int[] steps = new int[n];
        steps[1] = 2;
        steps[2] = 3;
        for (int i = 3; i < n; i++) {
            steps[i] = steps[i - 1] + steps[i - 2];
        }
        return steps[n - 1];
    }

    /**
     * 动态规划2
     */
    public int climbStairs3(int n) {
        if (n <= 3) return n;

        int[] steps = new int[2];
        steps[0] = 1;
        steps[1] = 2;
        for (int i = 2; i < n; i++) {
            steps[i % 3] = steps[(i - 1) % 3] + steps[(i - 2) % 3];
        }
        return steps[(n - 1) % 3];
    }
}
